package com.dsd.datav.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dsd.datav.domain.User;
import com.dsd.datav.dto.LoginUser;
import com.dsd.datav.mapper.UserMapper;
import com.dsd.datav.service.UserService;
import com.dsd.datav.utils.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author Administrator
 * @Title: UserServiceImpl
 * @ProjectName datav
 * @Description: TODO
 * @date 2023/12/29 00292023/12/29 0029
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private UserMapper userMapper;
    /** 加盐*/
    private static final String salt="dsd123456";
    @Override
    public LoginUser login(LoginUser loginUser) {
        User user= userMapper.findById(loginUser.getId());
        if (StringUtils.isEmpty(user)){
            return null;
        }else {
           /* String MD5Pwd = loginUser.getPassword() + salt;
            String s = DigestUtils.md5DigestAsHex(MD5Pwd.getBytes());*/
            if (user.getPassword().equals(loginUser.getPassword())){
                //登录成功后设置token
                String token = TokenUtil.sign(loginUser);
                loginUser.setToken(token);
                loginUser.setPassword(loginUser.getPassword());
                loginUser.setId(user.getId());
                loginUser.setName(user.getName());
                loginUser.setOrgId(user.getOrgId());
                loginUser.setTownship(user.getTownship());
                loginUser.setOrgName(user.getOrgName());
            }
            return userMapper.login(loginUser);
        }
    }
}
